package com.itheima.web.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.dao.IOrdersDao;
import com.itheima.domain.Orders;
import com.itheima.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    /**
     * 分页查询，查询所有订单
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllOrders")
    public ModelAndView findAllOrders(
            @RequestParam(name = "pageNum", required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", required = true, defaultValue = "3") Integer pageSize
    ) {
        List<Orders> ordersList = ordersService.findAllOrders(pageNum, pageSize);
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(ordersList);

        ModelAndView mav = new ModelAndView();
        mav.addObject("orders", pageInfo);
        mav.setViewName("orders-list");
        return mav;
    }


    @RequestMapping("/findByOrdersId")
    public String findByOrdersId(String id, Model model){
        Orders orders = ordersService.findByOrdersId(id);
        model.addAttribute("orders",orders);
        return "orders-show";
    }

}
